package com.aps.server.controller;

import com.aps.common.entity.Result;
import com.aps.server.entity.BasicProduct;
import com.aps.server.service.BasicProductService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.jsf.FacesContextUtils;

import java.util.List;
import java.util.Map;

/**
 * 产品信息控制器
 * 提供产品信息相关的REST API接口
 */
@Slf4j
@RestController
@RequestMapping("/api/basic-products")
@RequiredArgsConstructor
@Tag(name = "产品信息管理", description = "产品信息相关的API接口")
public class BasicProductController {
    @Resource
     BasicProductService basicProductService;

    /**
     * 创建产品信息
     */
    @PostMapping("/create")
    @Operation(summary = "创建产品信息", description = "创建一个新的产品信息")
    public Result<String> createProduct(@RequestBody BasicProduct basicProduct) {
        try {
            boolean success = basicProductService.createProduct(basicProduct);
            if (success) {
                return Result.success(basicProduct.getId().toString(), "产品信息创建成功");
            } else {
                return Result.error(400, "产品信息创建失败");
            }
        } catch (Exception e) {
            log.error("创建产品信息失败", e);
            return Result.error(500, "创建产品信息失败: " + e.getMessage());
        }
    }

    /**
     * 更新产品信息
     */
    @PostMapping("/modify")
    @Operation(summary = "更新产品信息", description = "根据产品ID更新产品信息")
    public Result<String> updateProduct(@RequestBody BasicProduct basicProduct) {
        try {
            String id = basicProduct.getId();
            if(id == null) {
                return Result.error(400, "数据错误","未获取到数据id！");
            }
            basicProduct.setId(id);
            boolean success = basicProductService.updateProduct(basicProduct);
            if (success) {
                return Result.success("产品信息更新成功");
            } else {
                return Result.error(400, "产品信息更新失败");
            }
        } catch (Exception e) {
            log.error("更新产品信息失败", e);
            return Result.error(500, "更新产品信息失败: " + e.getMessage());
        }
    }

    /**
     * 删除产品信息
     */
    @GetMapping("/del/{id}")
    @Operation(summary = "删除产品信息", description = "根据产品ID删除产品信息（使用配置的租户ID）")
    public Result<String> deleteProduct(
            @Parameter(description = "产品ID") @PathVariable("id") String id) {
        try {
            boolean success = basicProductService.deleteProduct(id);
            if (success) {
                return Result.success("产品信息删除成功");
            } else {
                return Result.error(400, "产品信息删除失败");
            }
        } catch (Exception e) {
            log.error("删除产品信息失败", e);
            return Result.error(500, "删除产品信息失败: " + e.getMessage());
        }
    }

    /**
     * 查询当前租户的产品列表
     */
    @GetMapping("/current-tenant")
    @Operation(summary = "查询当前租户的产品列表", description = "根据配置的租户ID查询所有产品")
    public Result<List<BasicProduct>> getProductsByCurrentTenant(
            @RequestParam(value = "productType", defaultValue = "", required = false) String productType,
            @RequestParam(value = "productName", defaultValue = "", required = false) String productName,
            @RequestParam(value = "productCode", defaultValue = "", required = false) String productCode,
            @RequestParam(value = "priority", defaultValue = "", required = false) String priority,
            @RequestParam(value = "status", defaultValue = "", required = false) String status
    ) {
        try {
            List<BasicProduct> products = basicProductService.getProductsByTenantId(productName,productCode,productType);
            return Result.success(products);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据当前租户ID查询产品失败", e);
            return Result.error(500, "查询产品失败: " + e.getMessage());
        }
    }
}